package zhuyun.code.swipe

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

/**
 * @author: Administrator
 * @date: 2022/1/6
 */
class SwipeRecyclerView(private val mContext: Context, private val attrs:AttributeSet?, private val defStyleAttr:Int): RecyclerView(mContext,attrs,defStyleAttr) {
    constructor(mContext: Context,attrs: AttributeSet?):this(mContext,attrs,0)
    constructor(mContext: Context):this(mContext,null)

    private var currentX = 0
    private var currentY = 0
    private var lastX = 0
    private var lastY = 0
    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        currentX = e.x.toInt()
        currentY = e.y.toInt()
        when(e.action){
            MotionEvent.ACTION_MOVE->{
                val dx = currentX - lastX
                val dy = currentY - lastY
                if(abs(dx)<abs(dy)){
                    return true
                }
            }

        }
        lastX = currentX
        lastY = currentY
        return super.onInterceptTouchEvent(e)
    }
}